package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import model.Administrador;
import model.Endereco;

public class AdministradorDAO extends PersistenciaDAO {

	public Boolean verificaAdministradorSenha(String email, String senha)
			throws SQLException {

		Boolean bool = Boolean.FALSE;

		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM pessoa WHERE ds_email = '" + email
						+ "' AND tp_pessoa = 'A' AND ds_senha = '" + senha
						+ "'");

		while (elements.next()) {
			if (elements.getString("ds_email") != null) {
				bool = Boolean.TRUE;
			}
		}
		return bool;
	}

	
	public Administrador buscarAdministradorPorId(int id) throws SQLException, Exception {

		Administrador adm = null;
		Statement sta = connection.createStatement();
		ResultSet elements = sta
				.executeQuery("SELECT * FROM pessoa WHERE cd_pessoa = " + id
						+ " AND tp_pessoa = 'A' ");

		while (elements.next()) {
			adm = montaAdministrador(elements);
		}
		return adm;
	}
	
	public Administrador montaAdministrador(ResultSet elements) throws SQLException, Exception {
		Administrador administrador = new Administrador();
		administrador.setId(elements.getInt("cd_pessoa"));
		administrador.setNome(elements.getString("nm_pessoa"));
		administrador.setCpf(elements.getString("nr_cpf"));
		administrador.setEmail(elements.getString("ds_email"));
		administrador.setSenha(elements.getString("ds_senha"));
		administrador.setProfissao(elements.getString("ds_profissao"));
		administrador.setDataNascimento(elements.getString("dt_nascimento"));

		Endereco endereco = new Endereco();
		endereco.setLogradouro(elements.getString("ds_logradouro"));
		endereco.setNumero(elements.getString("ds_numero"));
		endereco.setCidade(elements.getString("ds_cidade"));
		endereco.setCep(elements.getString("ds_cep"));
		administrador.setEndereco(endereco);

		return administrador;
	}
	
	public Collection<HashMap<String, Object>> buscarDadosAdministradorBrowse() throws SQLException {

		Collection<HashMap<String, Object>> result = new ArrayList<HashMap<String,Object>>();
		Statement sta = connection.createStatement();
		ResultSet elements = sta.executeQuery(
				" SELECT a.cd_pessoa as codigo, "+
				"  		a.nm_pessoa as descricao "+
				" FROM quiosque.pessoa a "+
				" WHERE a.tp_pessoa = 'A' "+
				" ORDER BY a.cd_pessoa ");
		
		while (elements.next()) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("codigo", elements.getInt("codigo"));
			map.put("descricao", elements.getString("descricao"));
			result.add(map);
		}
		return result;
	}

}
